package com.fire.market.order.impl;

import com.fire.component.dto.Response;
import com.fire.component.extension.Assert;
import com.fire.market.order.ProductIntegrationService;
import com.fire.market.product.api.dto.ItemInfo;
import com.fire.market.product.api.mq.ItemStockMqMessage;
import com.fire.market.product.api.request.ItemDecreaseParam;
import com.fire.market.product.api.service.ItemCmdFrontService;
import com.fire.market.product.api.service.ItemQueryFrontService;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.stereotype.Service;

/**
 * ProductIntegrationServiceImpl
 *
 * @author zwd
 * @date 2021/7/21
 */
@Service
public class ProductIntegrationServiceImpl implements ProductIntegrationService {
    @Reference
    ItemQueryFrontService itemQueryFrontService;

    @Reference
    ItemCmdFrontService itemCmdFrontService;

    @Override
    public ItemInfo getItemDetail(Integer itemId) {
        Assert.notNull(itemId, "itemId is null");
        return itemQueryFrontService.getItemDetail(itemId).getData();
    }

    @Override
    public boolean decreaseItemStockByCache(Integer itemId, Integer amount) {
        ItemDecreaseParam itemDecreaseParam = new ItemDecreaseParam();
        itemDecreaseParam.setAmount(amount);
        itemDecreaseParam.setItemId(itemId);
        return itemCmdFrontService.decreaseItemStockByCache(itemDecreaseParam).getData().isStatus();
    }

    @Override
    public void increaseSales(Integer itemId, Integer amount) {
        itemCmdFrontService.increaseSales(itemId, amount);
    }

    @Override
    public void syncDecreaseItemStockByMq(Integer itemId, Integer amount, String sequenceId, Integer userId) {
        ItemStockMqMessage itemStockMqMessage = new ItemStockMqMessage();
        itemStockMqMessage.setAmount(amount);
        itemStockMqMessage.setItemId(itemId);
        itemStockMqMessage.setUserID(userId);
        itemStockMqMessage.setOrderId(sequenceId);
        itemCmdFrontService.syncDecreaseItemStockByMq(itemStockMqMessage);
    }

    @Override
    public void increaseItemStockByCache(Integer itemId, Integer amount) {
        ItemDecreaseParam itemDecreaseParam = new ItemDecreaseParam();
        itemDecreaseParam.setAmount(amount);
        itemDecreaseParam.setItemId(itemId);
        Response response = itemCmdFrontService.increaseItemStockByCache(itemDecreaseParam);
    }

    @Override
    public void decreaseSales(Integer itemId, Integer amount) {
        ItemDecreaseParam itemDecreaseParam = new ItemDecreaseParam();
        itemDecreaseParam.setAmount(amount);
        itemDecreaseParam.setItemId(itemId);
        Response response = itemCmdFrontService.decreaseSales(itemDecreaseParam);

    }
}
